import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {
  // 1.初始化并获取数据

  // 2.调用生成工具
  useTool: (data) => ipcRenderer.invoke('use-tool', data),

  // 3.打开文件夹
  openFloder: (data) => ipcRenderer.invoke('open-folder', data),

  // 4.将生成的数据保存为docx文件
  saveDocx: (data) => ipcRenderer.invoke('save-docx', data)
}

// 5.数据存储
const store = {
  get: (key) => ipcRenderer.invoke('get-config', key),
  set: (key, value) => ipcRenderer.invoke('set-config', key, value),
  del: (key) => ipcRenderer.invoke('delete-config', key),
  // 一键打开配置目录
  openFolder: () => ipcRenderer.invoke('open-config-folder')
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
    contextBridge.exposeInMainWorld('store', store)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}
